package com.chat.service;

import com.chat.common.EmailMessage;
import com.chat.common.Message;
import com.chat.common.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class CheckSecurityCode {
    public boolean b=true;
    private static final Message msg=new Message();
    private static Socket socket;
    public boolean check(String code,String mailbox){
        try {
            socket=new Socket(InetAddress.getByName("127.0.0.1"),9999);
            ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
            msg.setMesType(MessageType.MESSAGE_CHECK_CODE);
            EmailMessage em=new EmailMessage();
            em.setContent(code);
            em.setAddress(mailbox);
            long time=System.currentTimeMillis();
            em.setTime(time);
            msg.setData(em);
            oos.writeObject(msg);

        } catch (IOException e) {
            e.printStackTrace();
        }
        ObjectInputStream ois= null;
        try {
            ois = new ObjectInputStream(socket.getInputStream());
            Message ms=(Message) ois.readObject();
            if(ms.getMesType().equals(MessageType.MESSAGE_CHECK_CODE))
            {
                   EmailMessage em1=new EmailMessage();
                   em1=(EmailMessage)ms.getData();
                b= em1.isState();
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
           return b;
    }


}
